package com.ruoyi.common.utils.wxlogin;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormatEmoji {

	public static String filterEmoji(String nick_name) {
		// nick_name 所获取的用户昵称
		if (nick_name == null) {
			return nick_name;
		}
		Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
				Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
		Matcher emojiMatcher = emoji.matcher(nick_name);
		if (emojiMatcher.find()) {
			// 将所获取的表情转换为*
			nick_name = emojiMatcher.replaceAll("*");
			return nick_name;
		}
		return nick_name;
	}

	public static String decodeUnicode(String unicode) {
		StringBuffer sb = new StringBuffer();
		String[] hex = unicode.split("\\\\u");
		for (int i = 1; i < hex.length; i++) {
			int data = Integer.parseInt(hex[i], 16);
			sb.append((char) data);
		}
		return sb.toString();

	}

	public static String decodeUnicode2(String dataStr) {
		int start = 0;
		int end = 0;
		final StringBuffer buffer = new StringBuffer();
		while (start > -1) {
			end = dataStr.indexOf("\\u", start + 2);
			String charStr = null;
			if (end == -1) {
				charStr = dataStr.substring(start + 2, dataStr.length());
			} else {
				charStr = dataStr.substring(start + 2, end);
			}
			char letter = (char) Integer.parseInt(charStr, 16);
			buffer.append(new Character(letter).toString());
			start = end;
		}
		return buffer.toString();

	}

}
